package weekimwee.cn.x5nightdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.tencent.smtt.sdk.WebSettings
import kotlinx.android.synthetic.main.activity_preview.*
import org.jsoup.Jsoup

class PreviewActivity : AppCompatActivity() {
    private var str =""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_preview)
        intent.getStringExtra("str")?.let {
            str = it
        }
        webView.settings.apply {
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN//把html中的内容放大webview等宽的一列中
            javaScriptEnabled = true
            builtInZoomControls = true
            setSupportZoom(true)
        }
        webView.loadDataWithBaseURL(null,getNewContent(intent.getStringExtra("str")), "text/html", "utf-8", "")
    }

    private fun getNewContent(htmltext: String): String {
        return try {
            val doc = Jsoup.parse(htmltext)
            val elements = doc.getElementsByTag("img")
            for (element in elements) {
                element.attr("width", "100%").attr("height", "auto")
            }

            doc.toString()
        } catch (e: Exception) {
            htmltext
        }

    }
}
